import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:image_picker/image_picker.dart';
import 'package:path_provider/path_provider.dart';

class PhotoApp extends StatefulWidget {
  @override
  _PhotoAppState createState() => _PhotoAppState();
}

class _PhotoAppState extends State<PhotoApp> {
  List<File> _image = [];
  static const platform = const MethodChannel('samples.flutter.io/battery');

  Future getImage(bool isCamera) async {
    Navigator.pop(context);
    var image = await ImagePicker.pickImage(
        source: isCamera ? ImageSource.camera : ImageSource.gallery);

    if (image != null) {
      setState(() {
        _image.add(image);
      });
    }
  }

  Future<String> _getBatteryLevel() async {
    String batteryLevel;
    final int result = await platform.invokeMethod('getPlatformVersion', "传递参数");
    batteryLevel = 'Battery level at $result % .';
    return batteryLevel;
  }

  @override
  Widget build(BuildContext context) {
    _getBatteryLevel().then((onValue) {
      print(onValue);
    });
    return Scaffold(
      appBar: AppBar(
        title: Text('Image Picker Example'),
      ),
      body: Center(
//          _image == null ? Text('No image selected.') : Image.file(_image)
        //child: Text('aas'),
        child: _image.length == 0 ? Text('No image selected.') : _imagesLayout(),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          _pickImage();
        },
        tooltip: 'Pick Image',
        child: Icon(Icons.add_a_photo),
      ),
    );
  }

  Widget _imagesLayout() {
    return Wrap(
      spacing: 5,
      runSpacing: 5,
      children: _image
          .map((f) => Stack(
                children: <Widget>[
                  Container(
                    width: 110,
                    height: 60,
                    child: ClipRRect(
                      borderRadius: BorderRadius.circular(3),
                      child: Image.file(
                        f,
                        fit: BoxFit.cover,
                      ),
                    ),
                  ),
                  Positioned(
                    right: 3,
                    top: 3,
                    child: InkWell(
                      child: ClipOval(
                        child: Container(
                          color: Colors.black26,
                          child: Icon(Icons.close, size: 18, color: Colors.white,),
                        ),
                      ),
                      onTap: () {
                        setState(() {
                          _image.remove(f);
                        });
                      },
                    ),
                  )
                ],
              ))
          .toList(),
//        children: <Widget>[
//          Container(
//            width: 160,
//            height: 100,
//            child: Image.file(
//              _image[0],
//              fit: BoxFit.cover,
//            ),
//          )
//        ],
    );
  }

  List<Widget> _list() {
    List<Widget> list = <Widget>[];
    for (File file in _image) {
      list.add(Stack(
        children: <Widget>[
          Container(
            width: 110,
            height: 60,
            child: ClipRRect(
              borderRadius: BorderRadius.circular(3),
              child: Image.file(
                file,
                fit: BoxFit.cover,
              ),
            ),
          ),
          Positioned(
            right: 3,
            top: 3,
            child: InkWell(
              child: Icon(Icons.close),
              onTap: () {
                setState(() {
                  _image.remove(file);
                });
              },
            ),
          )
        ],
      ));
    }
    return list;
  }

  _pickImage() {
    //   showModalBottomSheet(
//        context: context,
//        builder: (BuildContext bc) {
//          return Stack(
//            children: <Widget>[
//              Container(
//                height: 30.0,
//                width: double.infinity,
//                color: Colors.black54,
//              ),
//              Container(
//                decoration: BoxDecoration(
//                    color: Colors.white,
//                    borderRadius: BorderRadius.only(
//                      topLeft: Radius.circular(25),
//                      topRight: Radius.circular(25),
//                    )),
//              ),
//              Container(
//                child: FlatButton(
//                  child: Container(
//                    alignment: Alignment.center,
//                    padding:
//                    EdgeInsets.only(top: 33.0, bottom: 33.0),
//                    child: Text(
//                      "bottomSheet的内容",
//                    ),
//                  ),
//                ),
//              ),
//            ],
//          );
//        });
    showModalBottomSheet(
        context: context,
        builder: (context) {
          return Container(
            height: 120,
            child: ListView.separated(
                itemBuilder: (context, index) {
                  ListTile listTile;
                  if (index == 0) {
                    listTile = ListTile(
                      title: Text("拍照"),
                      leading: Icon(Icons.camera),
                      onTap: () {
                        getImage(true);
                      },
                    );
                  } else {
                    listTile = ListTile(
                      title: Text("从相册选择"),
                      leading: Icon(Icons.image),
                      onTap: () {
                        getImage(false);
                      },
                    );
                  }
                  return listTile;
                },
                separatorBuilder: (context, index) => Divider(
                      height: 1,
                    ),
                itemCount: 2),
          );
        });
  }
}
